﻿using System.Collections.Generic;
using System.IO;
using PerfAnalyzerLib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace UnitTests
{
    
    
    /// <summary>
    ///This is a test class for PerfLogTest and is intended
    ///to contain all PerfLogTest Unit Tests
    ///</summary>
	[TestClass()]
	public class PerfLogTest
	{
	    /// <summary>
	    ///Gets or sets the test context which provides
	    ///information about and functionality for the current test run.
	    ///</summary>
	    public TestContext TestContext { get; set; }

	    #region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion


		/// <summary>
		///A test for PerfLog Constructor.
		///</summary>
		[TestMethod]
		[DeploymentItem("PerfAnalyzer.dll")]
		public void PerfLog_ConstructorTest()
		{
			string filename = GeneratorCsvFile.GenerateEmptyFile();
			var target = new PerfLog(filename);

			Assert.AreEqual(filename, target.FileInfo.FullName);
		}

		/// <summary>
		///A test for Parse.
		///</summary>
		[TestMethod]
		public void PerfLog_ParseTest()
		{
			const int numberOfSample = 10;
			var sourceCounters = new List<Counter>();

			var counter = GeneratorCounter.GanarateStaticCounter(GeneratorCounterInfo.GenerateCounterInfo(1, 1, 1, 1), 0, numberOfSample);
			sourceCounters.Add(counter);

			counter = GeneratorCounter.GenerateIncrementalCounter(GeneratorCounterInfo.GenerateCounterInfo(1, 1, 1, 2), 10, numberOfSample);
			sourceCounters.Add(counter);

			counter = GeneratorCounter.GanarateStaticCounter(GeneratorCounterInfo.GenerateCounterInfo(1, 1, 1, 1), -10.5, numberOfSample);
			sourceCounters.Add(counter);

			string filename = GeneratorCsvFile.GenerateCsvFile(sourceCounters);

			var target = new PerfLog(filename);
			target.Parse();
			
			Assert.AreEqual(sourceCounters.Count, target.Counters.Count);

			for (int i = 0; i < sourceCounters.Count; i++)
			{
				Assert.AreEqual(sourceCounters[i].Info.Fullname, target.Counters[i].Info.Fullname);
				Assert.AreEqual(numberOfSample, target.Counters[i].Values.Length);
			}

			File.Delete(filename);
		}

		/// <summary>
		/// Test exception from empty file for the Parse method.
		/// </summary>
		[TestMethod]
		[ExpectedException(typeof(Exception))]
		public void PerfLog_Parse_EmptyFileExceptionTest()
		{
			string filename = GeneratorCsvFile.GenerateEmptyFile();
			var target = new PerfLog(filename);
			target.Parse();
		}

		/// <summary>
		/// Test of exception when a double cannot be parsed.
		/// </summary>
		[TestMethod]
		[ExpectedException(typeof(Exception))]
		public void PerfLog_Parse_CannotParseDoubleExceptionTest()
		{
			var content = new List<string>();
			content.Add("counter1,counter2");
			content.Add("10,20");
			content.Add("11,21");
			content.Add("12,wrong");

			string filename = GeneratorCsvFile.GenerateFile(content);

			var target = new PerfLog(filename);
			target.Parse();
		}

		/// <summary>
		/// Test mismatch number of columns resulting in an excpetion.
		/// </summary>
		[TestMethod]
		[ExpectedException(typeof(Exception))]
		public void PerfLog_Parse_ColumnNumberMismatchExceptionTest()
		{
			var content = new List<string>();
			content.Add("counter1,counter2");
			content.Add("10,20");
			content.Add("11,21");
			content.Add("12");

			string filename = GeneratorCsvFile.GenerateFile(content);

			var target = new PerfLog(filename);
			target.Parse();
		}

		/// <summary>
		/// Test for double quotes in some lines.
		/// </summary>
		[TestMethod]
		public void PerfLog_Parse_DoubleQuoteTest()
		{
			var content = new List<string>();
			content.Add("\"timestamp\",\"" + GeneratorCounterInfo.GenerateCounterInfo(1, 1, 1, 1) + "\",\"" + GeneratorCounterInfo.GenerateCounterInfo(1, 1, 1, 2) + "\"");
			content.Add("\"08/19/2012 7:11:12 am\",\"10\",\"20\"");
			content.Add("08/19/2012 7:11:13 am,11,21");
			content.Add("08/19/2012 7:11:14 am,12,22");

			string filename = GeneratorCsvFile.GenerateFile(content);

			var target = new PerfLog(filename);
			target.Parse();
		}

		/// <summary>
		/// Test a missing double quote resulting in having 2 columns in the headers and 3 for the rest.
		/// </summary>
		[TestMethod]
		[ExpectedException(typeof(Exception))]
		public void PerfLog_Parse_DoubleQuoteExceptionTest()
		{
			var content = new List<string>();
			content.Add("\"timestamp\",\"" + GeneratorCounterInfo.GenerateCounterInfo(1, 1, 1, 1) + "\",\"" + GeneratorCounterInfo.GenerateCounterInfo(1, 1, 1, 2) + "\"," + GeneratorCounterInfo.GenerateCounterInfo(1, 1, 1, 3) + "\"");
			content.Add("08/19/2012 7:11:12 am,10,20,30");
			content.Add("08/19/2012 7:11:13 am,11,21,31");
			content.Add("08/19/2012 7:11:14 am,12,22,32");

			string filename = GeneratorCsvFile.GenerateFile(content);

			var target = new PerfLog(filename);
			target.Parse();
		}

		/// <summary>
		/// Test exception when not enough columns are in perflog.
		/// </summary>
		[TestMethod]
		[ExpectedException(typeof(Exception))]
		public void PerfLog_Parse_NotEnoughColumnExceptionTest()
		{
			var content = new List<string>();
			content.Add("timestamp");
			content.Add("08/19/2012 7:11:12 am");
			content.Add("08/19/2012 7:11:13 am");
			content.Add("08/19/2012 7:11:14 am");

			string filename = GeneratorCsvFile.GenerateFile(content);

			var target = new PerfLog(filename);
			target.Parse();
		}

		/// <summary>
		/// Test exception for missing timestamp column.
		/// </summary>
		[TestMethod]
		[ExpectedException(typeof(Exception))]
		public void PerfLog_Parse_MissingTimeStampExceptionTest()
		{
			var content = new List<string>();
			content.Add("\""+ GeneratorCounterInfo.GenerateCounterInfo(1, 1, 1, 1) + "\",\"" + GeneratorCounterInfo.GenerateCounterInfo(1, 1, 1, 2) + "\"");
			content.Add("10,20,30");
			content.Add("11,21,31");
			content.Add("12,22,32");

			string filename = GeneratorCsvFile.GenerateFile(content);

			var target = new PerfLog(filename);
			target.Parse();
		}
	}
}